package com.miplus.ccrm.core.service.aiqm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.aiqm.*;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.QmTemplateMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【QM_TEMPLATE(质检模板)】的数据库操作Service实现
* @createDate 2024-11-03 23:01:34
*/
@Service
public class QmTemplateServiceImpl extends MPJBaseServiceImpl<QmTemplateMapper, QmTemplate>
    implements QmTemplateService{

    @Resource
    private QmTemprulesLinkService linkService;
    @Resource
    private QmModelRulesLinkService modelRulesLinkService;

    @Override
    public IPage<QmTempResVo> qmTempList(QmTempReqVo paras)
    {


        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                QmTempResVo.class,
                new MPJLambdaWrapper<QmTemplate>()
                        .like(JPubUtils.IsNotEmpty(paras.getTempname()),QmTemplate::getTemplatename,paras.getTempname())
                        .eq(paras.getTemplateid() != null, QmTemplate::getTemplateid, paras.getTemplateid())
                        .selectAll(QmTemplate.class)
        );
    }

    @Override
    public CmdResVo qmTempAdd(String $usrid, QmTempAddVo paras) {
        QmTemplate def = new QmTemplate();
        String checkmethod = paras.getCheckmethod(); // 质检方式
        BeanUtils.copyProperties(paras, def);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret = baseMapper.insert(def);
        linkService.getBaseMapper().delete(new QueryWrapper<QmTemprulesLink>()
                .eq("templateid", paras.getTemplateid())
        );
        // 如果是正则方式执行的流程，则保存规则
        if (checkmethod.equals("regex") && JPubUtils.IsNotEmpty(paras.getRulesList())) {
            for (Integer r : paras.getRulesList()) {
                QmTemprulesLink link = new QmTemprulesLink();
                link.setTemplateid(def.getTemplateid());
                link.setRulesid(r);
                linkService.getBaseMapper().insert(link);
            }
        } else if (checkmethod.equals("llm") && JPubUtils.IsNotEmpty(paras.getModelRulesList())) {
            // 如果是大模型方式执行的流程，则保存模型,不在这里新增规则
            for (Integer r : paras.getRulesList()) {
                QmModelRulesLink link = new QmModelRulesLink();
                link.setTemplateid(def.getTemplateid());
                link.setQamainsid(r);
                modelRulesLinkService.getBaseMapper().insert(link);
            }
        }
        return new CmdResVo(ret, def.getTemplateid().toString());
    }

    @Override
    public CmdResVo qmTempEdit(String $usrid,QmTempAddVo paras)
    {
        QmTemplate def=new QmTemplate();
        BeanUtils.copyProperties(paras,def);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.updateById(def);
        linkService.getBaseMapper().delete(new QueryWrapper<QmTemprulesLink>()
                .eq("templateid",paras.getTemplateid())
        );
        if(JPubUtils.IsNotEmpty(paras.getRulesList())) {
            for (Integer r : paras.getRulesList()) {
                QmTemprulesLink link=new QmTemprulesLink();
                link.setTemplateid(paras.getTemplateid());
                link.setRulesid(r);
                linkService.getBaseMapper().insert(link);
            }
        }
        if (paras.getCheckmethod().equals("llm")) {
            //创建质检主项
            QmQaMain main = new QmQaMain();
        }

        return new CmdResVo(ret,def.getTemplateid().toString());
    }

    @Override
    public CmdResVo qmTempDelete(Integer tempid)
    {
        // 先删除模板下的规则
        linkService.getBaseMapper().delete(new QueryWrapper<QmTemprulesLink>()
                .eq("templateid",tempid)
        );
        modelRulesLinkService.getBaseMapper().delete(new QueryWrapper<QmModelRulesLink>()
                .eq("templateid", tempid)
        );
        int ret=baseMapper.deleteById(tempid);
        return new CmdResVo(ret,tempid.toString());
    }

    @Override
    public List<LabelResVo> getTempList()
    {
        return selectJoinList(LabelResVo.class,
                new MPJLambdaWrapper<QmTemplate>()
                        .selectAs(QmTemplate::getTemplateid,LabelResVo::getValue)
                        .selectAs(QmTemplate::getTemplatename,LabelResVo::getLabel)
                        .selectAll(QmTemplate.class)
        );
    }
}




